<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Adds new child at the end of the children</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.domnode.html">« DOMNode</a></li>
      <li style="float: right;"><a href="domnode.c14n.html">DOMNode::C14N »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.domnode.html">DOMNode</a></li>
    <li>Adds new child at the end of the children</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="domnode.appendchild" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMNode::appendChild</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMNode::appendChild</span> &mdash; <span class="dc-title">
   Adds new child at the end of the children
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domnode.appendchild-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMNode::appendChild</strong></span>(<span class="methodparam"><span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span> <code class="parameter">$node</code></span>): <span class="type"><span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   This function appends a child to an existing list of children or creates
   a new list of children. The child can be created with e.g. 
   <span class="methodname"><a href="domdocument.createelement.html" class="methodname">DOMDocument::createElement()</a></span>,
   <span class="methodname"><a href="domdocument.createtextnode.html" class="methodname">DOMDocument::createTextNode()</a></span> etc. or simply by using
   any other node.
  </p>
  <p class="para">
   When using an existing node it will be moved.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domnode.appendchild-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">node</code></dt>

     <dd>

      <p class="para">
       The appended child.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domnode.appendchild-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The node added.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-domnode.appendchild-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   <dl>
    
     <dt>
<strong><code>DOM_NO_MODIFICATION_ALLOWED_ERR</code></strong></dt>

     <dd>

      <p class="para">
       Raised if this node is readonly or if the previous parent of the node
       being inserted is readonly.
      </p>
     </dd>

    
    
     <dt>
<strong><code>DOM_HIERARCHY_REQUEST_ERR</code></strong></dt>

     <dd>

      <p class="para">
       Raised if this node is of a type that does not allow children of the 
       type of the <code class="parameter">node</code> node, or if the node to 
       append is one of this node&#039;s ancestors or this node itself.
      </p>
     </dd>

    
    
     <dt>
<strong><code>DOM_WRONG_DOCUMENT_ERR</code></strong></dt>

     <dd>

      <p class="para">
       Raised if <code class="parameter">node</code> was created from a different 
       document than the one that created this node.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-domnode.appendchild-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   The following example will add a new element node to a fresh document.
   <div class="example" id="example-5419">
    <p><strong>示例 #1 Adding a child</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$node&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">"para"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newnode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-5420">
    <p><strong>示例 #2 Nested children</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$headNode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">"head"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$headNode</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$titleNode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$headNode</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$titleNode</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-domnode.appendchild-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domchildnode.after.html" class="methodname" rel="rdfs-seeAlso">DOMChildNode::after()</a> - Adds nodes after the node</span></li>
    <li class="member"><span class="methodname"><a href="domnode.insertbefore.html" class="methodname" rel="rdfs-seeAlso">DOMNode::insertBefore()</a> - Adds a new child before a reference node</span></li>
    <li class="member"><span class="methodname"><a href="domnode.removechild.html" class="methodname" rel="rdfs-seeAlso">DOMNode::removeChild()</a> - Removes child from list of children</span></li>
    <li class="member"><span class="methodname"><a href="domnode.replacechild.html" class="methodname" rel="rdfs-seeAlso">DOMNode::replaceChild()</a> - Replaces a child</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>